Imports cmplib01

Public Class frmElementPicker
    Inherits System.Windows.Forms.Form

    Private ptrGameData As CampaignerGameData
    Private ptrGroundChit As GroundChitType
    Private InitialPV As Boolean

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents pnlExistingElements As System.Windows.Forms.Panel
    Friend WithEvents lvwExistingElements As System.Windows.Forms.ListView
    Friend WithEvents colCount As System.Windows.Forms.ColumnHeader
    Friend WithEvents colElementName As System.Windows.Forms.ColumnHeader
    Friend WithEvents pnlAddElements As System.Windows.Forms.Panel
    Friend WithEvents btnRemoveElement As System.Windows.Forms.Button
    Friend WithEvents btnAddElement As System.Windows.Forms.Button
    Friend WithEvents btnOK As System.Windows.Forms.Button
    Friend WithEvents numRemoveCount As System.Windows.Forms.NumericUpDown
    Friend WithEvents numAddCount As System.Windows.Forms.NumericUpDown
    Friend WithEvents cboElementClass As System.Windows.Forms.ComboBox
    Friend WithEvents lstElements As System.Windows.Forms.ListBox
    Friend WithEvents lstOptionsForSelectedElement As System.Windows.Forms.ListBox
    Friend WithEvents lblElements As System.Windows.Forms.Label
    Friend WithEvents lblOptionsInSelectedElement As System.Windows.Forms.Label
    Friend WithEvents lblAddElement As System.Windows.Forms.Label
    Friend WithEvents tvwElementOptionTypes As System.Windows.Forms.TreeView
    Friend WithEvents chkAddOptions As System.Windows.Forms.CheckBox
    Friend WithEvents btnRemoveElementOption As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim ListViewItem1 As System.Windows.Forms.ListViewItem = New System.Windows.Forms.ListViewItem(New System.Windows.Forms.ListViewItem.ListViewSubItem() {New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, "30", System.Drawing.SystemColors.WindowText, System.Drawing.SystemColors.Window, New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))), New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, "Infantry Troop")}, -1)
        Dim ListViewItem2 As System.Windows.Forms.ListViewItem = New System.Windows.Forms.ListViewItem(New System.Windows.Forms.ListViewItem.ListViewSubItem() {New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, "60", System.Drawing.SystemColors.WindowText, System.Drawing.SystemColors.Window, New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))), New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, "Heavy Weapon")}, -1)
        Dim ListViewItem3 As System.Windows.Forms.ListViewItem = New System.Windows.Forms.ListViewItem(New System.Windows.Forms.ListViewItem.ListViewSubItem() {New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, "4", System.Drawing.SystemColors.WindowText, System.Drawing.SystemColors.Window, New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)))}, -1)
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(frmElementPicker))
        Me.pnlExistingElements = New System.Windows.Forms.Panel()
        Me.lblOptionsInSelectedElement = New System.Windows.Forms.Label()
        Me.lblElements = New System.Windows.Forms.Label()
        Me.btnRemoveElementOption = New System.Windows.Forms.Button()
        Me.lstOptionsForSelectedElement = New System.Windows.Forms.ListBox()
        Me.btnRemoveElement = New System.Windows.Forms.Button()
        Me.numRemoveCount = New System.Windows.Forms.NumericUpDown()
        Me.lvwExistingElements = New System.Windows.Forms.ListView()
        Me.colCount = New System.Windows.Forms.ColumnHeader()
        Me.colElementName = New System.Windows.Forms.ColumnHeader()
        Me.pnlAddElements = New System.Windows.Forms.Panel()
        Me.tvwElementOptionTypes = New System.Windows.Forms.TreeView()
        Me.lblAddElement = New System.Windows.Forms.Label()
        Me.chkAddOptions = New System.Windows.Forms.CheckBox()
        Me.btnAddElement = New System.Windows.Forms.Button()
        Me.numAddCount = New System.Windows.Forms.NumericUpDown()
        Me.cboElementClass = New System.Windows.Forms.ComboBox()
        Me.lstElements = New System.Windows.Forms.ListBox()
        Me.btnOK = New System.Windows.Forms.Button()
        Me.pnlExistingElements.SuspendLayout()
        CType(Me.numRemoveCount, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.pnlAddElements.SuspendLayout()
        CType(Me.numAddCount, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'pnlExistingElements
        '
        Me.pnlExistingElements.Anchor = ((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right)
        Me.pnlExistingElements.BackColor = System.Drawing.SystemColors.Control
        Me.pnlExistingElements.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.pnlExistingElements.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblOptionsInSelectedElement, Me.lblElements, Me.btnRemoveElementOption, Me.lstOptionsForSelectedElement, Me.btnRemoveElement, Me.numRemoveCount, Me.lvwExistingElements})
        Me.pnlExistingElements.Location = New System.Drawing.Point(211, 8)
        Me.pnlExistingElements.Name = "pnlExistingElements"
        Me.pnlExistingElements.Size = New System.Drawing.Size(200, 336)
        Me.pnlExistingElements.TabIndex = 20
        '
        'lblOptionsInSelectedElement
        '
        Me.lblOptionsInSelectedElement.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblOptionsInSelectedElement.Location = New System.Drawing.Point(8, 208)
        Me.lblOptionsInSelectedElement.Name = "lblOptionsInSelectedElement"
        Me.lblOptionsInSelectedElement.Size = New System.Drawing.Size(160, 16)
        Me.lblOptionsInSelectedElement.TabIndex = 27
        Me.lblOptionsInSelectedElement.Text = "Options for selected element"
        '
        'lblElements
        '
        Me.lblElements.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblElements.Location = New System.Drawing.Point(8, 3)
        Me.lblElements.Name = "lblElements"
        Me.lblElements.Size = New System.Drawing.Size(136, 16)
        Me.lblElements.TabIndex = 26
        Me.lblElements.Text = "Elements in this chit"
        '
        'btnRemoveElementOption
        '
        Me.btnRemoveElementOption.BackColor = System.Drawing.SystemColors.Control
        Me.btnRemoveElementOption.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnRemoveElementOption.Location = New System.Drawing.Point(116, 304)
        Me.btnRemoveElementOption.Name = "btnRemoveElementOption"
        Me.btnRemoveElementOption.Size = New System.Drawing.Size(75, 20)
        Me.btnRemoveElementOption.TabIndex = 25
        Me.btnRemoveElementOption.Text = "Remove"
        '
        'lstOptionsForSelectedElement
        '
        Me.lstOptionsForSelectedElement.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.lstOptionsForSelectedElement.Location = New System.Drawing.Point(8, 232)
        Me.lstOptionsForSelectedElement.Name = "lstOptionsForSelectedElement"
        Me.lstOptionsForSelectedElement.Size = New System.Drawing.Size(184, 67)
        Me.lstOptionsForSelectedElement.TabIndex = 24
        '
        'btnRemoveElement
        '
        Me.btnRemoveElement.BackColor = System.Drawing.SystemColors.Control
        Me.btnRemoveElement.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnRemoveElement.Location = New System.Drawing.Point(116, 176)
        Me.btnRemoveElement.Name = "btnRemoveElement"
        Me.btnRemoveElement.Size = New System.Drawing.Size(75, 20)
        Me.btnRemoveElement.TabIndex = 23
        Me.btnRemoveElement.Text = "Remove"
        '
        'numRemoveCount
        '
        Me.numRemoveCount.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.numRemoveCount.Location = New System.Drawing.Point(7, 176)
        Me.numRemoveCount.Maximum = New Decimal(New Integer() {5250, 0, 0, 0})
        Me.numRemoveCount.Minimum = New Decimal(New Integer() {1, 0, 0, 0})
        Me.numRemoveCount.Name = "numRemoveCount"
        Me.numRemoveCount.Size = New System.Drawing.Size(80, 20)
        Me.numRemoveCount.TabIndex = 22
        Me.numRemoveCount.Value = New Decimal(New Integer() {1, 0, 0, 0})
        '
        'lvwExistingElements
        '
        Me.lvwExistingElements.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.lvwExistingElements.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.colCount, Me.colElementName})
        Me.lvwExistingElements.FullRowSelect = True
        Me.lvwExistingElements.GridLines = True
        Me.lvwExistingElements.Items.AddRange(New System.Windows.Forms.ListViewItem() {ListViewItem1, ListViewItem2, ListViewItem3})
        Me.lvwExistingElements.Location = New System.Drawing.Point(8, 24)
        Me.lvwExistingElements.MultiSelect = False
        Me.lvwExistingElements.Name = "lvwExistingElements"
        Me.lvwExistingElements.Size = New System.Drawing.Size(184, 144)
        Me.lvwExistingElements.TabIndex = 0
        Me.lvwExistingElements.View = System.Windows.Forms.View.Details
        '
        'colCount
        '
        Me.colCount.Text = "Count"
        Me.colCount.Width = 40
        '
        'colElementName
        '
        Me.colElementName.Text = "Element Name"
        Me.colElementName.Width = 120
        '
        'pnlAddElements
        '
        Me.pnlAddElements.BackColor = System.Drawing.SystemColors.Control
        Me.pnlAddElements.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.pnlAddElements.Controls.AddRange(New System.Windows.Forms.Control() {Me.tvwElementOptionTypes, Me.lblAddElement, Me.chkAddOptions, Me.btnAddElement, Me.numAddCount, Me.cboElementClass, Me.lstElements})
        Me.pnlAddElements.Location = New System.Drawing.Point(5, 8)
        Me.pnlAddElements.Name = "pnlAddElements"
        Me.pnlAddElements.Size = New System.Drawing.Size(200, 336)
        Me.pnlAddElements.TabIndex = 19
        '
        'tvwElementOptionTypes
        '
        Me.tvwElementOptionTypes.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.tvwElementOptionTypes.CheckBoxes = True
        Me.tvwElementOptionTypes.ImageIndex = -1
        Me.tvwElementOptionTypes.Location = New System.Drawing.Point(8, 232)
        Me.tvwElementOptionTypes.Name = "tvwElementOptionTypes"
        Me.tvwElementOptionTypes.Nodes.AddRange(New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("Frag Grenades"), New System.Windows.Forms.TreeNode("Krak Grenades"), New System.Windows.Forms.TreeNode("Boltgun"), New System.Windows.Forms.TreeNode("Dreadnought Assault Cannon")})
        Me.tvwElementOptionTypes.SelectedImageIndex = -1
        Me.tvwElementOptionTypes.ShowLines = False
        Me.tvwElementOptionTypes.ShowPlusMinus = False
        Me.tvwElementOptionTypes.ShowRootLines = False
        Me.tvwElementOptionTypes.Size = New System.Drawing.Size(184, 96)
        Me.tvwElementOptionTypes.TabIndex = 28
        '
        'lblAddElement
        '
        Me.lblAddElement.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblAddElement.Location = New System.Drawing.Point(8, 4)
        Me.lblAddElement.Name = "lblAddElement"
        Me.lblAddElement.Size = New System.Drawing.Size(144, 16)
        Me.lblAddElement.TabIndex = 27
        Me.lblAddElement.Text = "Select Element Type to add"
        '
        'chkAddOptions
        '
        Me.chkAddOptions.Location = New System.Drawing.Point(8, 208)
        Me.chkAddOptions.Name = "chkAddOptions"
        Me.chkAddOptions.Size = New System.Drawing.Size(184, 16)
        Me.chkAddOptions.TabIndex = 26
        Me.chkAddOptions.Text = "Add Selected Options"
        '
        'btnAddElement
        '
        Me.btnAddElement.BackColor = System.Drawing.SystemColors.Control
        Me.btnAddElement.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnAddElement.Location = New System.Drawing.Point(117, 176)
        Me.btnAddElement.Name = "btnAddElement"
        Me.btnAddElement.Size = New System.Drawing.Size(75, 20)
        Me.btnAddElement.TabIndex = 21
        Me.btnAddElement.Text = "Add >>"
        '
        'numAddCount
        '
        Me.numAddCount.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.numAddCount.Location = New System.Drawing.Point(8, 176)
        Me.numAddCount.Minimum = New Decimal(New Integer() {1, 0, 0, 0})
        Me.numAddCount.Name = "numAddCount"
        Me.numAddCount.Size = New System.Drawing.Size(80, 20)
        Me.numAddCount.TabIndex = 2
        Me.numAddCount.Value = New Decimal(New Integer() {1, 0, 0, 0})
        '
        'cboElementClass
        '
        Me.cboElementClass.Location = New System.Drawing.Point(8, 24)
        Me.cboElementClass.Name = "cboElementClass"
        Me.cboElementClass.Size = New System.Drawing.Size(184, 21)
        Me.cboElementClass.TabIndex = 1
        Me.cboElementClass.Text = "(All Classes)"
        '
        'lstElements
        '
        Me.lstElements.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.lstElements.Items.AddRange(New Object() {"Infantry Troop", "Heavy Weapon", "Assault Weapon", "Armored Transport"})
        Me.lstElements.Location = New System.Drawing.Point(8, 48)
        Me.lstElements.Name = "lstElements"
        Me.lstElements.Size = New System.Drawing.Size(184, 119)
        Me.lstElements.TabIndex = 0
        '
        'btnOK
        '
        Me.btnOK.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right)
        Me.btnOK.BackColor = System.Drawing.SystemColors.Control
        Me.btnOK.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnOK.ForeColor = System.Drawing.Color.Black
        Me.btnOK.Location = New System.Drawing.Point(328, 352)
        Me.btnOK.Name = "btnOK"
        Me.btnOK.TabIndex = 28
        Me.btnOK.Text = "OK"
        '
        'frmElementPicker
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(416, 384)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnOK, Me.pnlExistingElements, Me.pnlAddElements})
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
        Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
        Me.Name = "frmElementPicker"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent
        Me.Text = "Add Elements To Unit"
        Me.pnlExistingElements.ResumeLayout(False)
        CType(Me.numRemoveCount, System.ComponentModel.ISupportInitialize).EndInit()
        Me.pnlAddElements.ResumeLayout(False)
        CType(Me.numAddCount, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Public Sub New(ByRef GameData As CampaignerGameData, ByRef Chit As GroundChitType, Optional ByVal InitialPointValue As Boolean = False)
        ' the InitialPointValue parameter effects how the point value is calculated (in comparrison to the current and initial)
        Me.New()

        InitialPV = InitialPointValue
        ptrGameData = GameData
        ptrGroundChit = Chit

        ' populate existing choices
        PopulateAddPanel()
        PopulateChitsElements()
        PopulateExistingOptionTypes()

    End Sub

    Private Sub PopulateAddPanel()
        Dim ElClasso As ElementClass

        cboElementClass.Items.Clear()

        ' add each of the choices
        cboElementClass.Items.Add("(All Classes)")
        cboElementClass.Text = "(All Classes)"
        For Each ElClasso In ptrGameData.ElementMan.DefinedElementClasses
            cboElementClass.Items.Add(ElClasso)
        Next

        PopulateExistingElementTypes(cboElementClass.Text)

    End Sub

    Private Sub PopulateExistingElementTypes(Optional ByVal ofType As String = "(All Classes)")
        Dim elTypo As ElementType

        lstElements.Items.Clear()

        For Each elTypo In ptrGameData.ElementMan.DefinedElementTypes
            If ofType = "(All Classes)" Then
                lstElements.Items.Add(elTypo)
            Else
                If elTypo.ElementClassName = ofType Then
                    lstElements.Items.Add(elTypo)
                End If
            End If
        Next
    End Sub

    Private Sub PopulateExistingOptionTypes(Optional ByVal ofType As String = "(All Classes)")
        Dim opto As ElementOptionType

        Try
            tvwElementOptionTypes.Nodes.Clear()
        Catch

        End Try

        For Each opto In ptrGameData.ElementMan.DefinedElementOptions
            If ofType = "(All Classes)" Then
                Dim nod As New TreeNode(opto.ElementOptionTypeName)
                tvwElementOptionTypes.Nodes.Add(nod)
            Else
                If opto.ElementClassName = ofType Then
                    Dim nod As New TreeNode(opto.ElementOptionTypeName)
                    tvwElementOptionTypes.Nodes.Add(nod)
                End If
            End If
        Next

    End Sub

    Private Sub cboElementClass_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboElementClass.SelectedIndexChanged
        PopulateExistingElementTypes(cboElementClass.Text)
        PopulateExistingOptionTypes(cboElementClass.Text)
    End Sub

    Private Sub btnAddElement_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddElement.Click
        If Not lstElements.SelectedItem Is Nothing Then
            AddElement(lstElements.SelectedItem, numAddCount.Value)
        Else
            MessageBox.Show("Select an element type from the list above.", "Select Element Type", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Private Sub btnRemoveElement_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemoveElement.Click

        Try
            If Not lvwExistingElements.SelectedItems(0) Is Nothing Then
                ' get the name of the selected guy
                Dim listi As ListViewItem = lvwExistingElements.SelectedItems(0)
                Dim itemIndex As Integer = lvwExistingElements.SelectedIndices(0)
                Dim elTypo As ElementType

                ReduceCountOfElement(itemIndex)
                Me.PopulateChitsElements()
            End If
        Catch ex As Exception

        End Try

    End Sub

    Private Sub ReduceCountOfElement(ByVal index As Integer)
        Dim ele As Element = ptrGroundChit.ChitElements.Item(index)

        ele.Count -= numRemoveCount.Value

        If ele.Count < 1 Then ptrGroundChit.ChitElements.Remove(ele)

    End Sub

    Private Sub PopulateChitsElements()
        ' list the count and type of elements this chit has.
        Dim Ele As Element

        lvwExistingElements.Items.Clear()

        For Each Ele In ptrGroundChit.ChitElements
            ' show the count, and the type in the listview
            Dim listI As New ListViewItem(Ele.Count)
            listI.SubItems.Add(Ele.ElementTypeName)
            lvwExistingElements.Items.Add(listI)
        Next

    End Sub

    Private Sub AddElement(ByVal elTypo As ElementType, ByVal count As Integer)
        ' if the element type already exists, add the count to the existing one.
        Dim bPreexisting As Boolean

        'if Not bPreexisting Then
        ' add the element
        Dim newEle As New GroupableElement
        newEle.Count = count
        newEle.GroupName = "<Ungrouped>"
        newEle.Description = elTypo.ElementDescription
        newEle.ElementTypeName = elTypo.ElementTypeName
        newEle.ElementBaseCost = elTypo.ElementPointValue
        ' check the element options
        If chkAddOptions.Checked Then
            ApplyOptionsToElement(newEle)
        End If

        ptrGroundChit.ChitElements.Add(newEle)
        'End If

        PopulateChitsElements()

    End Sub

    Private Sub ApplyOptionsToElement(ByRef newEle As Element)
        Dim nod As TreeNode

        For Each nod In tvwElementOptionTypes.Nodes
            If nod.Checked Then
                Dim opto As ElementOptionType
                For Each opto In ptrGameData.ElementMan.DefinedElementOptions
                    If opto.ElementOptionTypeName = nod.Text Then
                        ' add this element option to the provided element. Yeah man.
                        Dim ElOpt As New ElementOption()
                        ElOpt.ElementOptionClassName = opto.ElementClassName
                        ElOpt.ElementOptionCount = 1 ' default to 1
                        ElOpt.ElementOptionPointCost = opto.ElementPointCost
                        ElOpt.ElementOptionTypeName = opto.ElementOptionTypeName
                        newEle.Options.Add(ElOpt)
                        Exit For
                    End If
                Next
            End If
        Next

    End Sub

    Private Sub RemoveElement(ByVal elTypo As ElementType, ByVal count As Integer)
        Dim ele As Element

        Try
            For Each ele In ptrGroundChit.ChitElements
                If ele.ElementTypeName = elTypo.ElementTypeName Then
                    ' remove the shit
                    ele.Count -= count
                    If ele.Count <= 0 Then ptrGroundChit.ChitElements.Remove(ele)
                    PopulateChitsElements()
                    Exit For
                End If
            Next
        Catch ex As Exception

        End Try

    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        If Not InitialPV Then
            ' this is where we reassign the point value to the current point value. Yay!
            SetCurrentPointValue()
        End If
        Me.Hide()
    End Sub

    Private Sub SetCurrentPointValue()
        Dim ele As Element
        Dim pv As Integer

        For Each ele In ptrGroundChit.ChitElements
            pv += ele.TotalCost
        Next

        ptrGroundChit.CurrentPointValue = pv

    End Sub

    Private Sub lvwExistingElements_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvwExistingElements.SelectedIndexChanged
        SelectElement()
    End Sub

    Private Sub SelectElement()
        Try
            If Not lvwExistingElements.SelectedItems Is Nothing Then
                ' we populate this element's options

                Dim index As Integer = lvwExistingElements.SelectedIndices(0)
                Dim ele As Element = ptrGroundChit.ChitElements.Item(index)
                PopulateElementsOptions(ele)

            Else
                lstOptionsForSelectedElement.Items.Clear()
            End If
        Catch outta As ArgumentOutOfRangeException
            lstOptionsForSelectedElement.Items.Clear()
        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try
    End Sub

    Private Sub PopulateElementsOptions(ByRef ele As Element)
        lstOptionsForSelectedElement.Items.Clear()
        Dim opto As ElementOption
        For Each opto In ele.Options
            lstOptionsForSelectedElement.Items.Add(opto)
        Next
    End Sub

    Private Sub btnRemoveElementOption_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemoveElementOption.Click
        If Not lstOptionsForSelectedElement.SelectedItem Is Nothing Then
            Dim elnamo As String
            Dim opnamo As String

            Try
                elnamo = lvwExistingElements.SelectedItems(0).SubItems(1).Text
                opnamo = lstOptionsForSelectedElement.SelectedItem.ToString
                RemoveSelectedOptionFromElement(elnamo, opnamo)
            Catch ex As Exception
                HandleException(c_Module, ex)
            End Try
        End If
    End Sub

    Private Sub RemoveSelectedOptionFromElement(ByVal elementName As String, ByVal optionName As String)
        Dim Ele As Element
        Dim opto As ElementOption

        Try
            For Each Ele In ptrGroundChit.ChitElements
                If Ele.ElementTypeName = elementName Then
                    For Each opto In Ele.Options
                        If opto.ElementOptionTypeName = optionName Then
                            Ele.Options.remove(opto)
                            SelectElement()
                            Exit Sub
                        End If
                    Next
                End If
            Next
        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try

    End Sub

End Class
